var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');
var async = require('async');


var YjxPriceModel = function (obj) {
    YjxBaseModel.call(this,obj);

    // `addr_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '地址id',
    // `base_id` bigint(20) NOT NULL COMMENT '拷贝源地址id',
    // `addr_type` int(3) NOT NULL DEFAULT '0' COMMENT '地址类型：0用户地址管理, 1用户地址列表用, 10商品订单, 20发票订单',
    // `mobile` bigint(20) NOT NULL COMMENT '手机号',
    // `phone` bigint(20) DEFAULT NULL COMMENT '座机号',
    // `name` varchar(20) NOT NULL COMMENT '收货者',
    // `prov` bigint(20) DEFAULT NULL COMMENT '省会',
    // `city` bigint(20) DEFAULT NULL COMMENT '城市',
    // `dist` bigint(20) DEFAULT NULL COMMENT '区',
    // `detail` varchar(500) NOT NULL DEFAULT '' COMMENT '详细地址',
    // `user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
    // `default` int(1) NOT NULL DEFAULT '0' COMMENT '默认地址: 0否,1是',
    // `create_time` datetime NOT NULL,
    // `update_time` datetime NOT NULL,
    // `del_flg` int(1) NOT NULL DEFAULT '0',
    // PRIMARY KEY (`addr_id`),
    // KEY `Addr_idx_01` (`user_id`)

};

YjxPriceModel.prototype = new YjxBaseModel();

// 取得表名
YjxPriceModel.prototype.getTableName = function(){
    return 'Yjx_price';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxPriceModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'price_id='+this.price_id;
};


/**
 * 根据子模版id获取对应的商品信息以及商品价格(设计)
 * @author 李强
 * @date 2017-11-21
 * @param designId 用户唯一标识
 * @param orderType   支付类型
 * @param callback  回调
 *          err: 查询失败
 *          results : {}
 */
YjxPriceModel.prototype.getPayInfoFromDesign = function(designId,orderType,callback){
    pool.pool.getConnection(function(getConnectErr,connection) {
        if (getConnectErr) {
            console.log('YjxPriceModel.getPayInfoFromDesign getConnectErr: ' + getConnectErr);
            return callback(getConnectErr, null);
        }
        var sql = 'SELECT p.*,' +
            ' itdp.thumb AS displayThumb, ' +
            ' p.show_name AS itemDesc, '+
            ' itdd.design_name AS itemName, ' +
            ' 1 AS qty, ' +
            ' ts.width, ' +
            ' ts.height ' +
            ' FROM yjx.yjx_price p,' +
            ' yjx.yjx_price_r pr,' +
            ' yjx.yjx_uni_items yui,' +
            ' itbour.tg_design_page itdp, ' +
            ' itbour.tg_design_doc itdd, ' +
            ' itbour.tg_size ts ' +
            ' WHERE yui.item_id=' + designId +
            ' AND yui.item_id=itdp.design_id ' +
            ' AND itdd.design_id=itdp.design_id ' +
            ' AND itdp.page_index=0 ' +
            ' AND pr.uni_item_id=yui.uni_item_id ' +
            ' AND pr.price_id=p.price_id ' +
            ' AND p.price_type=' + orderType +
            ' AND yui.item_type=10 ' +
            ' AND p.state=10 ' +
            ' AND p.del_flg=0 ' +
            ' AND pr.del_flg=0 ' +
            ' AND yui.del_flg=0 ' +
            ' AND itdd.del_flg=0 ' +
            ' AND itdp.del_flg=0'+
            ' AND ts.size_id = itdd.size_id '+
            ' AND ts.del_flg = 0 '+
            ' ORDER BY p.unit_price asc';
        console.log(sql);
        connection.query(sql, function (error, results) {
            connection.release();
            if (error) {
                console.log('YjxPriceModel.getPayInfoFromDesign Err: ' + error);
                return callback(error, null);
            }
            return callback(null, results)
        })
    })
};


/**
 * 根据子模版id获取对应的商品信息以及商品价格(视频)
 * @author 李强
 * @date 2017-11-21
 * @param designId 用户唯一标识
 * @param priceType  (价格支付类型  2[去水印订单],4[秒杀订单],8[vip订单],16[待定]',)
 * @param callback  回调
 *          err: 查询失败
 *          results : {}
 */
YjxPriceModel.prototype.getPayInfoFromVideo = function(designId,priceType,callback){
    pool.pool.getConnection(function(getConnectErr,connection) {
        if (getConnectErr) {
            console.log('YjxPriceModel.getPayInfoFromVideo getConnectErr: ' + getConnectErr);
            return callback(getConnectErr, null);
        }
        var sql = ' SELECT p.*, ' +
            ' vdd.theme_thumb AS displayThumb, ' +
            ' 1 AS qty, ' +
            ' p.show_name AS itemDesc, '+
            ' vdd.design_name AS itemName, '+
            ' ts.width, '+
            ' ts.height '+
            ' FROM yjx.yjx_price p,' +
            ' yjx.yjx_price_r pr,' +
            ' yjx.yjx_uni_items yui,' +
            ' itbour.video_design_doc vdd, ' +
            ' itbour.tg_size ts ' +
            ' WHERE yui.item_id=' + designId +
            ' AND yui.item_id=vdd.design_id ' +
            ' AND pr.uni_item_id=yui.uni_item_id ' +
            ' AND pr.price_id=p.price_id ' +
            ' AND p.price_type=' + priceType +
            ' AND yui.item_type=30 ' +
            ' AND p.state=10 ' +
            ' AND p.del_flg=0 ' +
            ' AND pr.del_flg=0 ' +
            ' AND yui.del_flg=0 ' +
            ' AND vdd.del_flg=0'+
            ' AND ts.size_id = vdd.size_id'+
            ' AND ts.del_flg = 0 '+
            ' ORDER BY p.unit_price asc';
        console.log(sql);
        connection.query(sql, function (error, results) {
            connection.release();
            if (error) {
                console.log('YjxPriceModel.getPayInfoFromVideo Err: ' + error);
                return callback(error, null);
            }
            return callback(null, results)
        })
    })
};

// 获取商品套餐信息(如vip/充值包)
YjxPriceModel.prototype.getGoodsPriceInfo = function(uniItemId,priceId,goodsType,payType,callback){
    pool.pool.getConnection(function(getConnectErr,connection) {
        if (getConnectErr) {
            console.log('YjxPriceModel.getGoodsPriceInfo getConnectErr: ' + getConnectErr);
            return callback(getConnectErr, null);
        }

        var sql = ' SELECT ui.uni_item_id, yg.goods_id, p.price_id, yg.goods_name, yg.desc, yg.goods_type , yg.theme_thumb  ' +
            '      ,p.pay_type, p.unit_price, p.unit_pts, p.qty, p.unit, p.show_name , p.desc' +
            '      ,ui.sub_uni_item_id_1, ui.sub_uni_item_id_2, ui.sub_uni_item_id_3 ' +
            ' FROM yjx.yjx_goods yg ' +
            '     ,yjx.yjx_uni_items ui ' +
            '     ,yjx.yjx_price_r pr ' +
            '     ,yjx.yjx_price p ' +
            ' WHERE yg.del_flg=0 AND yg.state=20 '+// 已上架
            ' AND ui.del_flg=0 AND ui.item_type=40 AND ui.item_id=yg.goods_id '+// 商品
            ' AND pr.del_flg=0 AND pr.price_id=p.price_id AND pr.uni_item_id=ui.uni_item_id '+
            ' AND p.del_flg=0 AND p.state=10 '; // vip
        if(goodsType>0) {
            sql += ' AND yg.goods_type='+goodsType;
            if(goodsType==10){// VIP
                sql += ' AND p.price_type=8 ';
            } else if(goodsType==20) {// 充值包
                sql += ' AND p.price_type=16 ';
            }
        }
        if(payType>0) {
            sql += ' AND p.pay_type='+payType;
        }
        if(priceId>0) {
            sql += ' AND p.price_id=' + priceId;
        }
        if(uniItemId>0) {
            sql += ' AND ui.uni_item_id='+uniItemId;
        }
        sql += ' ORDER BY yg.goods_id, p.unit_price ';
        console.log(sql);

        connection.query(sql, function (error, results) {
            connection.release();
            if (error) {
                console.log('YjxPriceModel.getGoodsPriceInfo Err: ' + error);
                return callback(error, null);
            }
            return callback(null, results)
        })
    })
};


/**
 * 根据用户视频作品子表的作品 id 获取去水印的价格信息
 *
 * @author 王俊
 * @date 2018-03-26
 *
 * @param docId     视频作品子表 id
 * @param callback  回调
 *          err:    查询失败
 *          data:   price 行
 */
YjxPriceModel.prototype.getWmPriceInfoForUserVideoDocWithDocId = function (docId, callback) {
    pool.pool.getConnection(function(getConnectErr,connection) {
        if (getConnectErr) {
            console.log('YjxPriceModel.getWmPriceInfoForUserVideoDocWithDocId getConnectErr: ' + getConnectErr);
            return callback(getConnectErr, null);
        }
        var sql =
            ' SELECT yp.* ' +
            ' FROM yjx.yjx_video_user_doc yvud, ' +
            '      yjx.yjx_uni_items yui, ' +
            '      yjx.yjx_price_r ypr, ' +
            '      yjx.yjx_price yp ' +
            ' WHERE yvud.doc_id = ' + docId +
            '   AND yui.item_type = 30 ' +
            '   AND yui.item_id = yvud.design_id ' +
            '   AND ypr.uni_item_id = yui.uni_item_id ' +
            '   AND yp.price_id = ypr.price_id ' +
            '   AND ypr.del_flg = 0 ' +
            '   AND yp.del_flg = 0 ' +
            '   AND yvud.del_flg = 0 ' +
            '   AND yui.del_flg = 0 ';
        console.log(sql);
        connection.query(sql, function (error, results) {
            connection.release();
            if (error) {
                console.log('YjxPriceModel.getWmPriceInfoForUserVideoDocWithDocId Err: ' + error);
                return callback(error, null);
            }
            if(results && results.length > 0) {
                return callback(null, results[0]);
            }
            return callback(null, null);
        })
    })
};


module.exports = YjxPriceModel;

